using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SolarWinds.Helpers;
using SolarWinds.Input;

namespace SolarWinds.Widgets
{
    public class CheckBox : Button
    {
        public CheckBox(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            Checkable = true;
        }

        protected internal override void PaintEvent()
        {
            base.PaintEvent();

            if (Checked)
            {
                Rectangle srcRect = TileIndex2Rectangle(new Point(5, 6));
                Rectangle destRect = new Rectangle(ScreenPosition.X, ScreenPosition.Y, mTileSize.X, mTileSize.Y);

                SpriteBatch.Draw(mTileTexture, mDestRects[0], srcRect, Color.White);
            }

            // strip string
            String stripCaption = SpriteFontHelper.StripString(Font, Caption, Width - 2 * mTileSize.X);

            Vector2 fontSize = Font.MeasureString(stripCaption);

            Vector2 pos = new Vector2(ScreenPosition.X + 2.0f * mTileSize.X,
                                      ScreenPosition.Y + (Height - fontSize.Y) * 0.5f);

            SpriteBatch.DrawString(Font, stripCaption, pos, TextColor);
        }

        protected override void InitializeRects()
        {
            mSrcRects = new Rectangle[1];
            mDestRects = new Rectangle[1];
            mTileColors = new Color[1] { Color.Tomato };
        }

        protected override void UpdateSrcDestRects()
        {
            // box
            mSrcRects[0] = TileIndex2Rectangle(new Point(1, 0));
            mDestRects[0] = new Rectangle(ScreenPosition.X, ScreenPosition.Y, mTileSize.X, mTileSize.Y);
        }

        protected internal override void MouseOverEvent(MouseEvent evt)
        {
            mTileColors[0] = Color.Gray;
        }

        protected internal override void MouseOutEvent(MouseEvent evt)
        {
            mTileColors[0] = Color.Tomato;
        }

        protected internal override void KeyFocusInEvent(KeyEvent evt)
        {
            mTileColors[0] = Color.CornflowerBlue;
        }

        protected internal override void KeyFocusOutEvent(KeyEvent evt)
        {
            mTileColors[0] = Color.White;
        }
    }
}